<?php
class Arm_Form_Abstract extends Zend_Form{

    //Para apenas registrar o forme uma unica vez
    protected $registred    = false;

    protected $view;

    public $viewScriptPath  = null;
    public $viewScript      = null;
    public $translate       = null;

    public function __construct($options = null){
        //set tranlate
        $this->translate        = Zend_Registry::get('Zend_Translate');
        
        if(is_array($options)){
            $this->setOptions($options);
        }elseif($options instanceof Zend_Config){
            $this->setConfig($options);
        }

        $this->view = new Zend_View();
        $this->init();
    }

    public function init(){
        $this->viewScriptPath   = APPLICATION_PATH . "/../library/Arm/views/forms";
        $this->viewScript       = 'default.phtml';
    }

    /* Seta o path do script
     * EX: $this->setPath(__FILE__);
     */
    public function setPath($f){
    
        $path = pathinfo($f);
        $this->viewScriptPath   = dirname($path['dirname']) . '/views/forms';
        $this->viewScript       = strtolower($path['filename']) . '.phtml';
        
    }

    public function getElements(){
    
        if(!$this->registred){
            $this->register();
            $this->registred = true;
        } 
        
        return parent::getElements();
    }

    public function addElementsForm(Arm_Form_Abstract $form){
    
        $this->addElements($form->getElements());
    }

    // renderiza tag form se for passado a action pelo setAction
    public function renderNodeForm($content){
        if($this->getAction() == '') return $content;
        $tag = '<form 
                id="'     . $this->getName()   . '"
                action="' . $this->getAction() . '" 
                method="' . $this->getMethod() . '" 
                >';
        $tag .= $content;
        $tag .= '</form>';
        return $tag;
    }

    /* Method for abstract */
    public function pre_render(){
    
    }

    public function isValid($values){
    
        if(!$this->registred){
            $this->register();
            $this->registred = true;
        } 
    
        return parent::isValid($values);
    }

    public function render(Zend_View_Interface $view = null){
        
        if(!$this->registred){
            $this->register();
            $this->registred = true;
        } 
       
        $this->pre_render();
        
        $this->view->legend   = $this->getLegend();
        $this->view->name     = $this->getName();
        $this->view->elements = $this->getElements();
        $this->view->groups   = $this->getDisplayGroups();
        $this->view->forms    = $this->getSubForms();
        $this->view->action   = $this->getAction();
        $this->view->attribs  = $this->getAttribs();
        $this->view->setScriptPath($this->viewScriptPath);
        
        $this->view->translate  = $this->translate;
        
        $content = $this->view->render($this->viewScript);
        return $this->renderNodeForm($content);
    }

    protected function register(){
        
        throw new Zend_Exception('Arm_Form_Abstract::register() => Method not implemented');
    }
}
